package com.example.demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class JokeAdapter extends RecyclerView.Adapter<JokeAdapter.JokeViewHolder> {
    private Context context;

    private List<String> jokes;

    public JokeAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<String> jokes){
        this.jokes = jokes;
        notifyDataSetChanged();
    }

    @NonNull
    @NotNull
    @Override
    public JokeViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new JokeViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.item_joke, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull JokeViewHolder holder, int position) {
        holder.jokeText.setText(jokes.get(position));
    }

    @Override
    public int getItemCount() {
        return jokes == null ? 0 : jokes.size();
    }

    class JokeViewHolder extends RecyclerView.ViewHolder{

        TextView jokeText;

        public JokeViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            jokeText = itemView.findViewById(R.id.joke);
        }
    }
}
